/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */

package space.glsl;

import javax.media.opengl.GL3bc;
import jinngine.math.Vector3;
import space.gl.RenderContext;
import space.math.Vector3f;

/**
 *
 * @author Pierre
 */
public class GLSLDirectionalLight {

    final String osDirectionName;
    final String ambientColorName;
    final String diffuseColorName;
    final Vector3 direction;
    final Vector3f ambientColor;
    final Vector3f diffuseColor;

    public GLSLDirectionalLight(final String structInstanceName, final Vector3 direction, final Vector3f ambientColor, final Vector3f diffuseColor) {
        if (structInstanceName == null) {
            throw new IllegalArgumentException();
        }
        if (direction == null) {
            throw new IllegalArgumentException();
        }
        if (diffuseColor == null) {
            throw new IllegalArgumentException();
        }
        if (ambientColor == null) {
            throw new IllegalArgumentException();
        }
        this.osDirectionName = structInstanceName + ".osDirection";
        this.ambientColorName = structInstanceName + ".ambientColor";
        this.diffuseColorName = structInstanceName + ".diffuseColor";
        this.direction = direction;
        this.ambientColor = ambientColor;
        this.diffuseColor = diffuseColor;
    }

    public void applyUniform(final RenderContext rc) {
        final Vector3 osDirection = new Vector3();
        rc.worldToObject(direction, osDirection);
        rc.applyUniform3f(osDirectionName, osDirection);
        rc.applyUniform3f(ambientColorName, ambientColor);
        rc.applyUniform3f(diffuseColorName, diffuseColor);
    }

    public void draw(GL3bc gl) {
        float length = 50;
        gl.glBegin(gl.GL_LINES);
        gl.glColor3f(diffuseColor.x, diffuseColor.y, diffuseColor.z);
        gl.glVertex3f(0, 0, 0);
        gl.glVertex3f((float) direction.x * length,
                (float) direction.y * length,
                (float) direction.z * length);
        gl.glEnd();
    }

    public void setDirection(Vector3 rotate) {
        direction.assign((float) rotate.x, (float) rotate.y, (float) rotate.z);
    }
}
